<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model 
{
	private $tblUser = "users";	
	private $tblPrevAddr = "previous_addresses";
	private $tblMailingAddr = "mailing_addresses";
	private $tblJointUser = "joint_users";
	private $tblEmployer = "employers";
	private $tblBusiness = "business_details";
	
	protected $reserved_user = array('admin', 'administrator', 'root');
	
	function get_countries(){
		$this->db->select('ID, NAME, AREA_CODE');
		$this->db->from('countries');
		$query = $this->db->get();
		return $query->result();
	}

	function get_country($ID){
	  if($ID){
		$this->db->select('NAME');
		$q = $this->db->get_where('countries', array('ID' => $ID));
		$result = $q->row();
		if($result)
			return $result->NAME;
		else return false;
	  }
	  else{
		return false;
	  }
	}
	
	function get_current_years($ID){
		if($ID){
			$result = "";
			switch ($ID){
				case "1" :{
					$result = "0 - 1";
					break;
				}
				case "2": {
					$result = "1 - 2";
					break;
				}
				case "3": {
					$result = "2 - 3";
					break;
				}
				case "4": {
					$result = "3 +";
					break;
				}
			}
			return $result;
		  }
		  else{
			return false;
		  }
	}
	
	function get_anual_income($ID) {
		if($ID){
			$result = "";
			switch ($ID){
				case "1" :{
					$result = "Less than $25,000";
					break;
				}
				case "2": {
					$result = "$25,000 - $49,999";
					break;
				}
				case "3": {
					$result = "$50,000 - $99,999";
					break;
				}
				case "4": {
					$result = "$100,000 - $199,999";
					break;
				}
				case "5": {
					$result = "$200,000 +";
					break;
				}
			}
			return $result;
		  }
		  else{
			return false;
		  }
	}
	
	function get_saving_invest($ID) {
		if($ID){
			$result = "";
			switch ($ID){
				case "1" :{
					$result = "Less than $10,000";
					break;
				}
				case "2": {
					$result = "$10,000 - $24,999";
					break;
				}
				case "3": {
					$result = "$25,000 - $49,999";
					break;
				}
				case "4": {
					$result = "$50,000 - $99,999";
					break;
				}
				case "5": {
					$result = "$100,000 - $499,000";
					break;
				}
				case "6": {
					$result = "$500,000 - $999,000";
					break;
				}
				case "7": {
					$result = "$1,000,000 +";
					break;
				}
			}
			return $result;
		  }
		  else{
			return false;
		  }
	}
	
	
	function get_found_source($ID) {
		if($ID){
			$result = "";
			switch ($ID){
				case "1" :{
					$result = "Employment";
					break;
				}
				case "2": {
					$result = "Inheritance";
					break;
				}
				case "3": {
					$result = "Investments";
					break;
				}
				default:{
					$result = $ID;
				}
			}
			return $result;
		  }
		  else{
			return false;
		  }
	}
	
	function get_availability_ftype($ID) {
		if($ID){
			$result = "";
			$this->db->select('TYPE');
			$q = $this->db->get_where('available_funds_type', array('ID' => $ID));
			$result = $q->row();
			return $result->TYPE;
		  }
		  else{
			return false;
		  }
	}
	
	function is_property_owner($ID) {
		if($ID){
			$result = "";
			switch ($ID){
				case "1" :{
					$result = "Yes";
					break;
				}
				case "0": {
					$result = "No";
					break;
				}
			}
			return $result;
		  }
		  else{
			return false;
		  }
	}

	function get_frequencies(){
	    $this->db->select('ID, FREQUENCY');
	    $this->db->from('frequency');
	    $query = $this->db->get();
	    return $query->result();
	}

	function get_time_period(){
	    $this->db->select('ID, PERIOD');
	    $this->db->from('time_period');
	    $query = $this->db->get();
	    return $query->result();
	}
	
	function get_frequency($ID){
	    $this->db->select('FREQUENCY');
	    $query = $this->db->get_where('frequency', array('ID' => $ID));
	    $res = $query->row();
	    if($res){
	    	return $res->FREQUENCY;
	    }
	    else{
	    	return "";
	    }
	}

	function get_tperiod($ID){
	    $this->db->select('PERIOD');
	    $query = $this->db->get_where('time_period', array('ID' => $ID));
	    $res = $query->row();
	    if($res){
	    	return $res->PERIOD;
	    }
	    else{
	    	return "";
	    }
	}

	function get_available_ftypes(){
	    $this->db->select('ID, TYPE');
	    $this->db->from('available_funds_type');
	    $query = $this->db->get();
	    return $query->result();
	}
	
	function savePrevAddr($prevAddr = NULL) {
		if($prevAddr){
			$this->db->insert($this->tblPrevAddr, $prevAddr);
			return $this->db->insert_id();

		}
		return false;
	}
	
	function saveMailingAddr($mailingAddr = NULL) {
		if($mailingAddr){
			$this->db->insert($this->tblMailingAddr, $mailingAddr);
			return $this->db->insert_id();

		}
		return false;
	}
	
	function saveEmployer($employer) {
		if($employer){
			$this->db->insert($this->tblEmployer, $employer);
			return $this->db->insert_id();

		}
		return false;
	}
	
	function saveJointUser($jUser) {
		if($jUser){
			$this->db->insert($this->tblJointUser, $jUser);
			return $this->db->insert_id();

		}
		return false;
	}
	
	function saveUser($user) {
		if($user){
//			echo "<br />iata un apel la funtie";
			$this->db->insert($this->tblUser, $user);
			return $this->db->insert_id();

		}
		return false;
	}
	
	function saveBusiness($bizz) {
		if($bizz){
			$this->db->insert($this->tblBusiness, $bizz);
			return $this->db->insert_id();
		}
		return false;
	}
	
	function check_user_exist($login = NULL){
		if(in_array($login, $this->reserved_user)){
			return -1;
		}
		$this->db->where("USER_NAME", $login);
		$this->db->from($this->tblUser);
		$count = $this->db->count_all_results();

		if($count > 0){
			return -1;
		}
		else{
			return 1;
		}
	}

}


class User{
  public $USER_NAME = "";
  public $USER_PASSWD= ""; 
  public $TITLE = ""; 
  public $FIRST_NAME = ""; 
  public $MIDDLE_NAME = "";  
  public $LAST_NAME = "";  
  public $MAIL_ADDRESS = ""; 
  public $SEC_Q = "";  
  public $SEC_Q_ANSWER = "";  
  public $ACCOUNT_PREFERENCES_ID = "";  
  public $ACCOUNT_TYPE_ID = "";  
  public $BASE_CURRENCY = ""; 
  public $MANAGED_AUTHORIZED = "";
    
  public $COUNTRY_CODE = ""; 
  public $ADDRESS_MAIN = "";  
  public $ADDRESS_SECOND = "";  
  public $CITY = "";  
  public $STATE = ""; 
  public $POSTAL_CODE = "";  
  public $CURRENT_ADDR_YEARS  = ""; 
  public $HOME_PHONE = "";  
  public $MOBILE_PHONE = "";  
  
  public $PREVIOUS_ADDR_ID = "";  
  public $SEPARATE_MAILING = "";  
  public $MAILING_ADDRESS = "";  
  
  public $BIRTHDATE = "";  
  public $CITIZENSHIP_COUNTRY = "";  
  public $ISSUING_COUNTRY = "";
  public $PASS_NUMBER = "";
  public $EXPIRY_DATE = "";
  
  public $EMPLOYMENT_STATUS = "";  
  public $EMPLOYER_ID = "";  
  public $INCOME_TYPE_ID = "";  
  public $SAVINGS_TYPE_ID = "";  
  public $FUND_SOURCE = "";  
  public $FUNDS_AV_TYPE = ""; 
  public $PROPERTY_OWNER = "";  
  public $PROPERTY_VALUE = "";  
  
  public $INITIAL_DEPOSIT = "";  
  public $SHARES_BONS_FREQUENCY = ""; 
  public $SHARES_BONS_PERIOD = ""; 
  public $DERIVATIVES_FREQUENCY = ""; 
  public $DERIVATIVES_PERIOD = ""; 
  public $OTC_DERIVATIVES_FREQUENCY  = ""; 
  public $OTC_DERIVATIVES_PERIOD = "";  
  public $KNOWLEDGE_QUALIFICATION = "";  
  public $TRADING_EXPERIENCE = "";  
  public $PROFESSIONAL_EDUCATION = "";
  public $REFERER = "";
  
  
  function get_account_type_ID($ID){
 	 if($ID){
			$result = "";
			switch ($ID){
				case "individual" :{
					$result = "1";
					break;
				}
				case "joint_survivorship": {
					$result = "3";
					break;
				}
				case "joint_common": {
					$result = "4";
					break;
				}
				case "corporate": {
					$result = "2";
					break;
				}
			}
			return $result;
		  }
		  else{
			return false;
		  }
  }
}

class PrevAddress {
	public $USER_ID = "";
	public $JOINT_USER_ID = "";
	public $COUNTRY_CODE = ""; 
  	public $ADDRESS_MAIN = "";
  	public $ADDRESS_SECOND = ""; 
  	public $CITY = ""; 
  	public $STATE = ""; 
  	public $POSTAL_CODE = ""; 
}

class MailingAddress {
	public $USER_ID = "";
	public $JOINT_USER_ID = ""; 
	public $BUSINESS_USER_ID = ""; 
	public $COUNTRY_CODE = "";
  	public $ADDRESS_MAIN = ""; 
  	public $ADDRESS_SECOND = ""; 
  	public $CITY = ""; 
  	public $STATE = ""; 
  	public $POSTAL_CODE = "";
}

class Employer {
	public $E_NAME = ""; 
  	public $COUNTRY_ID = ""; 
  	public $ADDRESS_MAIN = "";
  	public $ADDRESS_SECOND = "";
  	public $CITY = "";
  	public $STATE = "";
  	public $POSTAL_CODE = ""; 
  	public $BUSINESS_NATURE = "";
  	public $E_POSITION = "";
  	public $OVERSEAS_REGULATOR = ""; 	
}

class JointUser {
	public $USER_ID = "";
	public $TITLE = "";
  	public $FIRST_NAME = ""; 
  	public $MIDDLE_NAME = "";
  	public $LAST_NAME = "";
  	public $COUNTRY_CODE = "";
  	public $ADDRESS_MAIN = "";
  	public $ADDRESS_SECOND = "";
  	public $CITY = "";
  	public $STATE = "";
  	public $POSTAL_CODE = ""; 
  	public $CURRENT_ADDR_YEARS = "";
  	public $HOME_PHONE = "";
  	public $MOBILE_PHONE = "";
  	 
  	public $PREVIOUS_ADDR_ID = "";
  	public $SEPARATE_MAILING = "";
  	public $MAILING_ADDRESS = "";
  	
  	public $BIRTHDATE = "";
  	public $CITIZENSHIP_COUNTRY = ""; 
  	public $ISSUING_COUNTRY = "";
  	public $PASS_NUMBER = "";
  	public $EXPIRY_DATE = "";
  	
  	public $EMPLOYMENT_STATUS = ""; 
  	public $EMPLOYER_ID = "";
  	
  	public $INCOME_TYPE_ID = "";
  	public $SAVINGS_TYPE_ID = "";
  	public $FUND_SOURCE = "";
  	public $FUNDS_AV_TYPE = "";
  	public $PROPERTY_OWNER = "";
  	public $PROPERTY_VALUE = "";
  	
  	public $SHARES_BONS_FREQUENCY = ""; 
  	public $SHARES_BONS_PERIOD = "";
  	public $DERIVATIVES_FREQUENCY = "";
  	public $DERIVATIVES_PERIOD = "";
  	public $OTC_DERIVATIVES_FREQUENCY = ""; 
  	public $OTC_DERIVATIVES_PERIOD = "";
  	public $KNOWLEDGE_QUALIFICATION = "";
  	public $TRADING_EXPERIENCE = "";
  	public $PROFESSIONAL_EDUCATION = "";
}


class BusinessUser {
	public $USER_ID = "";
	public $COMPANY_NAME = "";
  	public $REG_NUMBER = "";
  	public $INCORP_COUNTRY = "";
  	public $INCORP_DATE = "";
  	public $REG_COUNTRY = "";
  	public $ADDRESS_MAIN = "";
  	public $ADDRESS_SECOND = "";
  	public $CITY = "";
  	public $STATE = "";
  	public $POSTAL_CODE = "";
  	public $PHONE = "";
  	public $MOBILE_PHONE = "";
  	public $SEPARATE_MAILING = "";
  	public $POSITION_PRINCIPAL = "";
  	public $OFF1_NAME = "";
  	public $OFF1_POSITION = "";
  	public $OFF2_NAME = "";
  	public $OFF2_POSITION = "";
  	public $OFF3_NAME = "";
  	public $OFF3_POSITION = "";
  	public $BUSINESS_TYPE = "";
  	public $REG_INVESTMENT = "";
  	public $REGULATOR = "";
  	public $BREG_NUMBER = "";
  	public $ANUAL_REVENUE = "";
  	public $NETWORTH = "";
  	public $BANK_NAME = "";
  	public $BANK_COUNTRY = "";
  	public $BANK_ADDRESS_MAIN = "";
  	public $BANK_ADDRESS_SECOND = "";
  	public $BANK_CITY = "";
  	public $BANK_STATE = "";
  	public $BANK_POSTAL_CODE = "";
  	public $BANK_ACCOUNT_NAME = "";
  	public $BANK_ACCOUNT_NUMBER = "";
  	public $BANK_SORT_CODE = "";
	
}
